package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.MyUserService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("myUser")
public class MyUserController {

    @Reference
    private MyUserService myUserService;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    /*
     * @Author: hyy
     * @Description:添加方法
     * @Date: 20:19 2020/6/30
     * @param: null
     * @return:
     * @Version: 1.0
     */
    @RequestMapping("add")
    public Result add(@RequestParam(value = "roleIds")Integer[] roleIds, @RequestBody User user){

        try {
            user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
            myUserService.add(roleIds,user);
            return new Result(true,"添加用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加用户失败");
        }

    }

    /*
     * @Author: hyy
     * @Description:根本username查询对应的user数据
     * @Date: 20:20 2020/6/30
     * @param: null
     * @return:
     * @Version: 1.0
     */
    @RequestMapping("findUserByName")
    public Result findUserByName(@RequestParam("username") String username){

        try {
            User user = myUserService.findUserByName(username);
            if (user!=null) {
                return new Result(false,"用户名已存在");
            }else {
                return new Result(true,"用户名可用");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询出错");
        }

    }

    /*
     * @Author: hyy
     * @Description:根据id删除对应的user数据
     * @Date: 20:20 2020/6/30
     * @param: null
     * @return:
     * @Version: 1.0
     */
    @RequestMapping("deleteById")
    public Result deleteById(@RequestParam("id") Integer id){

        try {
            myUserService.deleteById(id);
            return new Result(true,"删除用户成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除用户失败");
        }

    }

    /*
     * @Author: hyy
     * @Description:分页查询用户数据
     * @Date: 20:21 2020/6/30
     * @param: queryPageBean
     * @return: com.itheima.entity.Result
     * @Version: 1.0
     */
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){

        try {
            PageResult pageResult =myUserService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
           List<User> rows = pageResult.getRows();
          /*  for (User row : rows) {
                Date Datebirth = row.getBirthday();
                String format = new SimpleDateFormat("yyyy-MM-dd").format(Datebirth);
                String birthday = DateUtils.parseDate2String(Datebirth);
                Date date = DateUtils.parseString2Date(birthday, "yyyy-MM-dd");

            }*/
            return new Result(true,"查询用户成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询用户失败");
        }
    }

    /*
     * @Author: hyy
     * @Description:根据id查询对应的user数据
     * @Date: 20:21 2020/6/30
     * @param: null
     * @return:
     * @Version: 1.0
     */
    @RequestMapping("findById")
    public Result findById(@RequestParam("id")Integer id){
        try {
            User user = myUserService.findById(id);
            return new Result(true,"查找用户成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查找用户失败");
        }
    }

    /*
     * @Author: hyy
     * @Description:编辑用户数据
     * @Date: 20:22 2020/6/30
     * @param: null
     * @return:
     * @Version: 1.0
     */
    @RequestMapping("edit")
    public Result edit(@RequestParam(value = "roleIds")Integer[] roleIds, @RequestBody User user){
        try {
            user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
            myUserService.edit(roleIds,user);
            return new Result(true,"修改用户信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改用户信息成功");
        }
    }

    /*
     * @Author: hyy
     * @Description:根据id查询对应的角色信息
     * @Date: 20:22 2020/6/30
     * @param: null
     * @return:
     * @Version: 1.0
     */
    @RequestMapping("findRoleIdsByUserId")
    public Result findRoleIdsByUserId(@RequestParam("id") Integer id){
        try {
            List<Integer> RoleIds=myUserService.findRoleIdsByUserId(id);
            return new Result(true,"查询角色成功",RoleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色失败");
        }
    }



}
